package com.igame.Controller;

import com.igame.Service.CategoriesService;
import com.igame.Service.CodeService;
import com.igame.Service.CommonService;
import com.igame.Utils.KaptchaUtil;
import com.igame.common.Result;
import com.igame.vo.CategoriesVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;


@CrossOrigin //忽视跨域问题
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Autowired
    private CodeService codeService;
    @Autowired
    CategoriesService categoriesService;
    @Autowired
    CommonService commonService;
    @PostMapping("/upload")
    public Result<String> upload(@RequestBody MultipartFile file){
        String upload = commonService.upload(file);
        if (upload!=null){
          Result.success(upload);
      }
        return Result.error("失败");
    }

    @GetMapping("/kaptcha")
    public Map<String, String> getCaptcha() throws Exception {
        KaptchaUtil kaptchaUtil =new KaptchaUtil();
        Map<String, String> response = kaptchaUtil.defaultKaptcha();
        String code = response.get("code");
        codeService.setCode("code",code);
        return response;
    }
    @GetMapping("/getCategories")
    public Result<List<CategoriesVo>> getCategories(){
        return categoriesService.selectCategories();
    }
}